package com.richpath.animator;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.view.animation.Interpolator;

import java.util.List;

public abstract class RichAnimator<T extends RichAnimator> {
    /**
     * This value used with the {@link AnimationBuilder#repeatCount(int)} property to repeat
     * the animation indefinitely.
     */
    public static final int INFINITE = -1;
    protected T prev;
    protected T next;
    protected long duration = -1;
    protected long startDelay = -1;
    protected Interpolator interpolator;
    protected RepeatMode repeatMode = RepeatMode.UNSET;
    protected int repeatCount = -2;
    protected AnimatorSet animatorSet;
    protected AnimationListener animationListener;

    public abstract List<Animator> createAnimatorList();

    private AnimatorSet createAnimatorSet() {
        List<Animator> animators = createAnimatorList();
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animators);
        if (duration != -1) {
            animatorSet.setDuration(duration);
        }
        if (startDelay != -1) {
            animatorSet.setStartDelay(startDelay);
        }

        if (interpolator != null) {
            animatorSet.setInterpolator(interpolator);
        }

        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (animationListener != null) {
                    animationListener.onStart();
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (animationListener != null) {
                    animationListener.onStop();
                }
                if (next != null) {
                    next.prev = null;
                    next.start();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });

        return animatorSet;
    }

    void start() {
        if (prev != null) {
            prev.start();
        } else {
            animatorSet = createAnimatorSet();
            animatorSet.start();
        }
    }

    public void cancel() {
        if (animatorSet != null) {
            animatorSet.cancel();
        }
        if (next != null) {
            next.cancel();
            next = null;
        }
    }

    void duration(long duration) {
        this.duration = duration;
    }

    void startDelay(long startDelay) {
        this.startDelay = startDelay;
    }

    void interpolator(Interpolator interpolator) {
        this.interpolator = interpolator;
    }

    void repeatMode(RepeatMode repeatMode) {
        this.repeatMode = repeatMode;
    }

    void repeatCount(int repeatCount) {
        this.repeatCount = repeatCount;
    }

    void setAnimationListener(AnimationListener animationListener) {
        this.animationListener = animationListener;
    }

    public enum RepeatMode {
        RESTART(1), REVERSE(2), UNSET(-2);
        final int valueNum;

        RepeatMode(int i) {
            valueNum = i;
        }
    }
}
